package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.livebuyer.parameter.QueryLiveProductsBean;
import com.ymatou.iapi.livebuyer.service.QueryLiveProductsCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ModifyLiveActivityBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*批量查询直播下的部分商品
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryLiveProducts{
	private static QueryLiveProductsBean queryliveproductsBean;
	private static QueryLiveProductsCall queryliveproductsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询直播下的部分商品");
	}

	@Before
	public void caseUp() {
		queryliveproductsBean=new QueryLiveProductsBean();
		queryliveproductsCall=new QueryLiveProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryliveproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Map<Integer, List<String>> m, Integer productnum) {
		JsonObject data = queryliveproductsCall.getJsonObject().get("data").getAsJsonObject();
		Logger.debug("map:"+m);
		m.keySet().forEach( key->{
			List<String> list = m.get(key);
			Logger.debug(key+":key-list:"+list);
			JsonArray ja = data.get(key.toString()).getAsJsonArray();
//			Logger.debug(ja.toString());
			boolean has=false;
			for (JsonElement json : ja) {
				if(list.contains(json.getAsJsonObject().get("productId").getAsString())){
					has=true;
				}
			}
			Logger.verifyEquals(true, has, "判断结果包含商品");
		});
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryLiveProducts_001() {
		Logger.start(true,"批量查询直播下的部分商品成功");
		try {
			//商品现在有商家限制 需要下降后新商品上架 UPDATE dbo.Ymt_Products SET validEnd = GETDATE() - 1 WHERE iUserId = 3383 
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			List<String> pids=new ArrayList<String>();
			pids.add(pid);
			m.put(createlive.getId(), pids);
			Integer productnum=2;
			List<Integer> liveIdList=new ArrayList<Integer>();
			liveIdList.add(createlive.getId());
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsBean.setProductNum(2);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("200", queryliveproductsCall.getString("code"), "判断code是否为200");
			
			verifyDB(m,productnum);
			
//			Logger.verifyEquals(pid, data.get("productId").getAsString(), "判断置顶商品");
//			Logger.verifyEquals("true", data.get("top").getAsString(), "判断置顶商品");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryLiveProducts_002() {
		Logger.start(true,"批量查询多个直播下的部分商品查询成功");
		try {
			//可能会出现重复
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			List<String> pids=new ArrayList<String>();
			List<Integer> liveIdList=new ArrayList<Integer>();
			for (int i = 0; i < 1; i++) {
				//由于发布权限问题导致商品发布失败 现在就一个商家
				List<Userinfo> list = new JUserWapper().select1KSeller();
				Userinfo uinfo = list.get(i);
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
				LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
				pids.add(pid);
				m.put(createlive.getId(), pids);
				liveIdList.add(createlive.getId());
			}
			
			Integer productnum=2;
			
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsBean.setProductNum(2);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("200", queryliveproductsCall.getString("code"), "判断code是否为200");
			
			verifyDB(m,productnum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryLiveProducts_003() {
		Logger.start(true,"批量查询直播下的多个部分商品查询成功");
		try {
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			List<String> pids=new ArrayList<String>();
			List<Integer> liveIdList=new ArrayList<Integer>();
			for (int i = 0; i < 1; i++) {
				List<Userinfo> list = new JUserWapper().select1KSeller();
				Userinfo uinfo = list.get(0);
				LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
				for(int j=0;j<2;j++){
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
				pids.add(pid);
				}
				m.put(createlive.getId(), pids);
				liveIdList.add(createlive.getId());
			}
			
			Integer productnum=2;
			
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsBean.setProductNum(2);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("200", queryliveproductsCall.getString("code"), "判断code是否为200");
			
			verifyDB(m,productnum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryLiveProducts_004() {
		Logger.start(true,"批量查询直播下的多个部分商品查询成功,直播商品总数超过显示数");
		try {
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			List<String> pids=new ArrayList<String>();
			List<Integer> liveIdList=new ArrayList<Integer>();
			for (int i = 0; i < 1; i++) {
				List<Userinfo> list = new JUserWapper().select1KSeller();
				Userinfo uinfo = list.get(0);
				LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
				for(int j=0;j<5;j++){
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
				pids.add(pid);
				}
				m.put(createlive.getId(), pids);
				liveIdList.add(createlive.getId());
			}
			
			Integer productnum=4;
			
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsBean.setProductNum(2);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("200", queryliveproductsCall.getString("code"), "判断code是否为200");
			
			verifyDB(m,productnum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryLiveProducts_005() {
		Logger.start(true,"批量查询直播下的部分商品成功,直播无商品");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
//			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
//			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
//			List<String> pids=new ArrayList<String>();
//			pids.add(pid);
//			m.put(createlive.getId(), pids);
			Integer productnum=2;
			List<Integer> liveIdList=new ArrayList<Integer>();
			liveIdList.add(createlive.getId());
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsBean.setProductNum(2);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("200", queryliveproductsCall.getString("code"), "判断code是否为200");
			Logger.verifyEquals("{}", queryliveproductsCall.getString("data"), "判断data");
//			verifyDB(m,productnum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryLiveProducts_006() {
		Logger.start(true,"不输入liveIdList失败");
		try {
			queryliveproductsBean.setProductNum(2);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("100", queryliveproductsCall.getString("code"), "判断code是否为200");
			Logger.verifyIsContains("错误的请求参数",queryliveproductsCall.getString("message"), "判断message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QueryLiveProducts_007() {
		Logger.start(true,"不输入productNum失败");
		try {
			List<Integer> liveIdList=new ArrayList<>();
			liveIdList.add(1);
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("100", queryliveproductsCall.getString("code"), "判断code是否为200");
			Logger.verifyIsContains("错误的请求参数",queryliveproductsCall.getString("message"), "判断message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryLiveProducts_008() {
		Logger.start(true,"批量查询直播下的部分商品成功-优先出秒杀、置顶商品");
		try {
			
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品："+pid);
			addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid2 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品-置顶："+pid2);
			addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid3 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品-秒杀："+pid3);
			
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid2);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid3);
			
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(pid3).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, new Date(now.getTime() + 10 * 1000));
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			
			LiveRoomCallService.SetLiveProductTop(createlive.getId(), pid2);
			Map<Integer,List<String>> m=new HashMap<Integer,List<String>>();
			List<String> pids=new ArrayList<String>();
			pids.add(pid);
			pids.add(pid2);
			pids.add(pid3);
			m.put(createlive.getId(), pids);
			Integer productnum=3;
			List<Integer> liveIdList=new ArrayList<Integer>();
			liveIdList.add(createlive.getId());
			queryliveproductsBean.setLiveIdList(liveIdList);
			queryliveproductsBean.setProductNum(productnum);
			queryliveproductsCall.setData(queryliveproductsBean);
			queryliveproductsCall.callService();			
			Logger.verifyEquals("200", queryliveproductsCall.getString("code"), "判断code是否为200");
			
			verifyDB(m,productnum);
			JsonArray ja = queryliveproductsCall.getJsonObject().get("data").getAsJsonObject().get(createlive.getId().toString()).getAsJsonArray();
			//顺序为 秒杀-置顶-普通
			Logger.verifyEquals(pid3, ja.get(0).getAsJsonObject().get("productId").getAsString(), "验证秒杀");
			Logger.verifyEquals(pid2, ja.get(1).getAsJsonObject().get("productId").getAsString(), "验证置顶");
			Logger.verifyEquals(pid, ja.get(2).getAsJsonObject().get("productId").getAsString(), "验证普通");
			
//			Logger.verifyEquals(pid, data.get("productId").getAsString(), "判断置顶商品");
//			Logger.verifyEquals("true", data.get("top").getAsString(), "判断置顶商品");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}